/**
 * 功能描述 总表格触发事件
 */
define(function (require, exports, module) {
	var store = require("store");
	var activeUser = store.get("user");
	var Dialog=require("dialog");
	var aw = require("ajaxwrapper");
	
    module.exports = {
    	//报名
    	signup : function(data, widget){
    		var startDate = data.startDate;
    		var date = moment().valueOf();
    		if(date>=startDate){
    			Dialog.alert({
    				tip : "提示",
    				content : "报名时间已经过了，已经无法报名了"
    			})
    			return false;
    		}
			var subnav = widget.get("subnav");
			var servicePoint = subnav.getData("servicePoint")[0];
			
			widget.get("form").setData(data);
			widget.get("form").setValue("year",data.year.substring(0,4));
			widget.get("form").setValue("destination",data.dname);
			widget.get("form").setValue("pkServicePoint",servicePoint.pkServicePoint);
			widget.get("form").setValue("servicePoint",servicePoint.name);
			widget.get("editgrid").setData(null);
			if(data.signUpRegister!=null){
				widget.get("editgrid").setData(data.signUpRegister[0].planItemGroup.signUpRoomTypeCounts);
			}
			
			widget.show([".J-form",".J-editgrid",".J-applygrid"]).hide(".J-grid");
			widget.get("subnav").show(["cancel","save"]).hide(["travelDate", "destination", "servicePoint","payFul"]);
			$(".J-grid-head-formel").removeClass("hidden");
			widget.get("applyGrid").refresh();
			
        },
        detail : function(data, widget){
        	var startDate = data.startDate;
    		var date = moment().valueOf();
			var subnav = widget.get("subnav");
			var servicePoint = subnav.getData("servicePoint")[0];
			widget.get("form").setData(data);
			widget.get("form").setValue("year",data.year.substring(0,4));
			widget.get("form").setValue("destination",data.dname);
			widget.get("form").setValue("pkServicePoint",servicePoint.pkServicePoint);
			widget.get("form").setValue("servicePoint",servicePoint.name);
			widget.get("form").setDisabled(true);
			widget.show([".J-form", ".J-applygrid",".J-editgrid"]).hide(".J-grid");
			widget.get("subnav").show(["cancel"]).hide(["travelDate", "destination", "servicePoint","payFul"]);
			widget.get("editgrid").setData(data.signUpRegister[0].planItemGroup.signUpRoomTypeCounts);
			widget.get("editgrid").get("model").allowEdit=false;
			widget.get("editgrid").get("model").allowAdd=false;
			widget.get("applyGrid").refresh(null,function(){
				$(".J-grid-memberRegisterApplyGrid-edit").addClass("hidden");
				$(".J-grid-memberRegisterApplyGrid-delete").addClass("hidden");
			});
			$(".J-grid-head-formel").addClass("hidden");
        },
        confirm : function(data, widget){
        	if(data.limitmin && (data.countMember < data.minPeople)){
        		Dialog.alert({
            		title:"确认失败",
            		content:"报名人数未达最少人数限制，无法确认！"
            	});
        		return false;
        	}
        	if(data.signUpStatus==null&&data.countMember>0){
				var subnav = widget.get("subnav");
				Dialog.confirm({
					id:"confirm",
					title:"提示",
					content:"确认"+data.dname+" "+moment(data.startDate).format("YYYY-MM-DD")+" "+
					moment(data.endDate).format("YYYY-MM-DD")+"该团报名结束了吗？",
					confirm:function(){
						Dialog.close("confirm");
						Dialog.confirm({
							id:"updata",
							title:"提示",
							content:"请输入"+data.dname+" "+moment(data.startDate).format("YYYY-MM-DD")+" "+
							moment(data.endDate).format("YYYY-MM-DD")+"该团团费每人预收款金额："+
							"<input class='J-grid-updata' id='receivables' style='text-align:right' type='text' value=''>" +
							"<span id='signupregister-error' style='color:red;margin-left:8px;'></span>"+
							//"总金额:"+data.countMember*($(".J-grid-updata").val()==null?0:$(".J-grid-updata"))+
							"总金额:"+
							"<input class='J-grid-totalM'  style='text-align:right' type='text' readonly='readonly' value=''>" + 
							"<span id='signupregister-error' style='color:red;margin-left:8px;'></span>",
							events: {
	        					"change": function(){ 
	        						var value =  $(".J-grid-updata").val();
	   							    	var form = widget.get("form");
	            							var totalPrices = 0;
	            							totalPrices += value*data.countMember;
	                						$(".J-grid-totalM").val(totalPrices);
	        					}
	        				},
							confirm:function(){
								var tValue = $(".J-grid-updata").val();
								if(!tValue){
									$("#signupregister-error").text("预收款金额不能为空");
									$("#signupregister-error").show();
									return false;
								}
								if(isNaN(tValue)){
									$("#signupregister-error").text("输入的预收款金额不合法");
									$("#signupregister-error").show();
									return false;
								}
								if(tValue <= 0){
									$("#signupregister-error").text("只能输入正数");
									$("#signupregister-error").show();
									return false;
								}
								aw.ajax({
									url :"api/planitem/updata",
									data:{
										"pkPlanItem":data.pkPlanItem,
										 version:data.version,
										 pkServicePoint:widget.get("subnav").getValue("servicePoint"),
										 "receivables":$(".J-grid-updata").val()
									},
		 							dataType:"json",
									success : function(data){
										widget.get("grid").refresh();
										Dialog.close("updata");
										return false;
									}
								});
							}
						})
						return false;
					}
				});
			}else{
				Dialog.alert({
            		title:"确认失败",
            		content:"该状态下不能再进行确认！"
            	});
        		return false;
			}
        },
        charge:function(data,widget){
        	if(data.signUpStatus&&data.signUpStatus.key=="Confirm"){
				widget.openView({
					url:"eling/elcms/travel/signupcharge/src/signupcharge_main",
					params: {
					},
					isAllowBack:true
				});
			}else{
				Dialog.alert({
            		title:"收费失败",
            		content:"该状态下不能在进行收费！"
            	});
        		return false;
			}
        },
        review:function(data,widget){
        	if(data.signUpStatus==null&&data.countMember>0){
				var subnav = widget.get("subnav");
				Dialog.confirm({
					id:"confirm",
					title:"提示",
					content:" 确认已对（"+data.dname+" "+moment(data.startDate).format("YYYY-MM-DD")+" "+
					moment(data.endDate).format("YYYY-MM-DD")+"）该团报名人员一一进行电话确认，可以按时出发了吗？",
					confirm:function(){
						
						
						Dialog.close("confirm");
						return false;
					}
				});
			}else{
				Dialog.alert({
            		title:"复核失败",
            		content:"该状态下不能在进行复核！"
            	});
        		return false;
			}
        }
    };
});
